package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.common.Constants;
import com.example.common.enums.ApprovalStatusEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Travels;
import com.example.entity.User;
import com.example.mapper.PraiseMapper;
import com.example.mapper.TravelsMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TravelsService {
    @Resource
    private TravelsMapper travelsMapper;

    @Resource
    private PraiseMapper praiseMapper;

    /**
     * 新增
     */
    public void add(Travels travels) {
        travels.setReadCount(Constants.READ_COUNT); //阅读量默认为0
        travels.setTime(DateUtil.now()); //设置发布时间
        travels.setStatus(ApprovalStatusEnum.NOT_APPROVAL.getDesc()); //设置审核状态
        User currentUser = (User) TokenUtils.getCurrentUser();
        travels.setUserId(currentUser.getId()); //设置当前用户id
        travelsMapper.insert(travels);
    }

    /**
     * 更新
     */
    public void updateById(Travels travels) {
        Account currentUser = TokenUtils.getCurrentUser(); //获取当前登录用户信息
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            travels.setStatus(ApprovalStatusEnum.NOT_APPROVAL.getDesc()); //只要用户更新了游记的内容，那么审核状态就会变为待审核
        }
        travelsMapper.updateById(travels);
    }

    /**
     * 更新阅读量
     */
    public void updateReadCount(Integer id) {
        travelsMapper.updateReadCount(id);
    }

    /**
     * 删除
     */
    public void deleteByid(Integer id) {
        travelsMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
/*        for (Integer id : ids) {
            travelsMapper.deleteById(id);
        }*/
        travelsMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询
     */
    public Travels selectById(Integer id) {
        Travels travels = travelsMapper.selectById(id);
        setTravelData(travels);
        return travels;
    }

    /**
     * 查询所有（条件查询）
     */
    public List<Travels> selectAll(Travels travels) {
        return travelsMapper.selectAll(travels);
    }

    /**
     * 分页查询（在查询所有的基础上进行分页）
     */
    public PageInfo<Travels> selectPage(Travels travels,Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser(); //获取当前登录用户信息
        if (currentUser == null || RoleEnum.USER.name().equals(currentUser.getRole())) {
            travels.setStatus(ApprovalStatusEnum.AGREE.getDesc()); //游客 or 普通用户 在前端页面只能查看到审核通过的游记
        }

        PageHelper.startPage(pageNum,pageSize);
        List<Travels> list = travelsMapper.selectAll(travels);
        for (Travels t : list) {
            setTravelData(t);
        }
        return PageInfo.of(list);
    }

    /**
     * 分页查询（用户查询自己发布的游记）
     */
    public PageInfo<Travels> selectPersonPage(Travels travels, Integer pageNum, Integer pageSize) {
        User currentUser = (User) TokenUtils.getCurrentUser(); //获取当前登录用户信息
        travels.setUserId(currentUser.getId()); //设置用户Id
        PageHelper.startPage(pageNum,pageSize);
        List<Travels> list = travelsMapper.selectAll(travels);
        return PageInfo.of(list);
    }

    /**
     * 设置当前游记的点赞量
     */
    private void setTravelData(Travels travels) {
        Integer praiseCount = praiseMapper.selectCount(travels.getId()); //查询当前游记的点赞量
        travels.setPraiseCount(praiseCount); //设置当前游记的点赞量
    }
}
